package com.example.test_camera;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;

public class AttachImage extends Activity {

	static final int CALL_CAMERA = 0;
	static final int CALL_GALLERY = 1;
	ImageView mImage;
	String mPath;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attachimage);

		mImage = (ImageView) findViewById(R.id.imageView1);
		mPath = Environment.getExternalStorageDirectory().getAbsolutePath()
				+ "/attachimage.jpg";
	}

	public void mOnClick(View v) {
		Intent intent;

		switch (v.getId()) {
		case R.id.btncamera:
			intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

			// 고화질 사진 저장 EXTRA_OUTPUT
			intent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(mPath)));
			startActivityForResult(intent, CALL_CAMERA);
			break;

		case R.id.btngallery:
			intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, CALL_GALLERY);
			break;
		}
	}
	// 호출 액티비티
	// data를 통해 선택한 사진의 Uri가 리턴
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CALL_CAMERA:
				mImage.setImageBitmap((Bitmap)data.getExtras().get("data"));
				break;
			case CALL_GALLERY:
				try
				{
					mImage.setImageBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData()));
			    } catch (Exception e) {
			       ;
			       }    
	
				break;
			}
		}
	}


}